// const baseUrls = 'https://www.huiwanvip.com/';
// const baseUrls = 'http://my.jschunk.com:8085/';
// const baseUrls = 'http://soundlab.jschunk.com:8085/';
// const baseUrls = 'https://my.jschunk.com/'; 
const baseUrls='http://my.jschunk.com:8085/';
// const baseUrls = 'http://192.168.0.113:8085/'; 
var MD5 = require('md5');
const app = getApp()

var nextStr = '';

function changeDataType(obj) {
  let str = ''
  if (typeof obj == 'object') {
    for (let i in obj) {
      if (typeof obj[i] != 'function' && typeof obj[i] != 'object') {
        str += i + '=' + obj[i] + '&';
      } else if (typeof obj[i] == 'object') {
        nextStr = '';
        str += changeSonType(i, obj[i])
      }
    }
  }
  return str.replace(/&$/g, '');
}

function changeSonType(objName, objValue) {
  if (typeof objValue == 'object') {
    for (let i in objValue) {
      if (typeof objValue[i] != 'object') {
        let value = objName + '[' + i + ']=' + objValue[i];
        nextStr += encodeURI(value) + '&';
      } else {
        changeSonType(objName + '[' + i + ']', objValue[i]);
      }
    }
  }
  return nextStr;
}
var attachQuery = function (url, queries) {
  for (var i = 1, l = arguments.length; i < l; i++) {
    var q = arguments[i];
    console.log('qqqq', q, url)
    var char = url.indexOf('?') === -1 ? '?' : '&';
    var qs = q ? changeDataType(q) : '';
    if (qs === '') char = '';
    console.log('qs021321321313', qs)
    qs = decodeURI(qs);
    console.log('qs', qs, q)
    url += char + qs;
  }
  return url;
}

module.exports = {

  requestForm: function (needToken, method, url, datas, callback) {
    wx.getNetworkType({
      success: function (res) {
        var netType = res.networkType;
        if (netType != "none") {
          var header = {
            'content-type': 'application/json'
          }
          if (needToken) {
            if (!wx.getStorageSync('token')) {
              wx.navigateTo({
                url: '/pages/mine/my/my',
              });
              return;
            }
            header["authenticate"] = wx.getStorageSync('token');
            const result = wx.getStorageSync('result')
            app.globalData.result = result
            var user = app.globalData.result
            let signUrl = url;
            if (method === 'get') {
              datas._u_ = user._id;
              datas._t_ = new Date().getTime();
              console.log('url', url)
              signUrl = attachQuery(url, datas);
              datas = null;
            } else {
              signUrl = attachQuery(url, {
                _u_: user._id,
                _t_: new Date().getTime()
              });
            }
            // let query = {
            //   // datas,
            //   "_u_": user._id,
            //   "_t_": new Date().getTime()
            // };

            // let urls = url;

            console.log("signUrl:::::", signUrl)

            console.log("md5 Text:", "/" + signUrl + user.token);
            let _s_ = MD5.hexMD5("/" + signUrl + user.token);
            console.log("md5", _s_);
            signUrl = signUrl + '&_s_=' + _s_;
            console.log("url::::::::::::", url, signUrl);
            var urls = baseUrls + signUrl
          } else {
            var urls = baseUrls + url
            var datass = datas
          }
          wx.request({
            url: urls,
            method: method,
            data: datas,
            header: header,
            success: function (resp) {
              if (resp.data.code === 4011) {
                wx.clearStorageSync();
                wx.navigateTo({
                  url: '/pages/mine/my/my?redirect=1',
                })
              }
              console.log(url + '请求返回值', resp.data)
              
              typeof callback == "function" && callback(resp.data)
            },
            fail: function (resp) {
              // typeof callback == "function" && callback(app.serverStatus.requestFailData);
            }
          })
        } else {
          wx.showToast({
            title: '无网络连接',
            icon: 'none',
            duration: 1000,
            mask: true
          });
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '获取网络失败',
          icon: 'none',
          duration: 1000,
          mask: true
        });
      }
    });
  },

  // 上传
  uploadFile: function (filePath, callback) {
    wx.uploadFile({
      url: baseUrls + 'res/qiniu',
      filePath: filePath,
      name: 'file',
      // header: {
      //   'content-type': 'multipart/form-data'
      // },
      success: function (res) {
        // console.log('上传成功', res)
        typeof callback == "function" && callback(res)
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  //录音上传
  uploadrecord: function (filePath, bgm, callback) {
    wx.uploadFile({
      url: baseUrls + 'res/record?bgm=' + bgm,
      filePath: filePath,
      name: 'file',
      // header: {
      //   'content-type': 'multipart/form-data'
      // },
      success: function (res) {
        console.log('上传成功', res)
        typeof callback == "function" && callback(res)
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }


}